#include "mainwidget.h"

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
{
    layout = new QGridLayout(this);

    for(int i=0; i<8; i++) {
        keyPressFlag[i] = false;
    }

    lblGameWindow = new GameWindowLabel(this);

    spinMaxLvl = new QSpinBox(this);
    spinMaxLvl->setRange(1, 50);
    spinMaxLvl->setAlignment(Qt::AlignCenter);
    spinMaxLvl->setPrefix("难度等级：");
    spinMaxLvl->setFixedHeight(30);
    spinMaxLvl->setFocusPolicy(Qt::NoFocus);

    QSettings settings(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);
    spinMaxLvl->setValue(settings.value("MaxLvl", 1).toInt());

    connect(spinMaxLvl, QOverload<int>::of(&QSpinBox::valueChanged), this, [=](int value) {
        QSettings settings(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);
        settings.setValue("MaxLvl", value);
    });

    lblStatistics = new QLabel("正确率: 0% (0 / 0)");
    lblStatistics->setFixedHeight(30);
    lblStatistics->setAlignment(Qt::AlignCenter);
    lblStatistics->setStyleSheet("background-color:white");

    layout->addWidget(lblGameWindow, 0, 0, 1, 2);
    layout->addWidget(spinMaxLvl, 1, 0);
    layout->addWidget(lblStatistics, 1, 1);

    layout->setMargin(0);
    layout->setSpacing(0);

    setFixedSize(800, 800);
    setWindowTitle("打字小游戏");
}

int MainWidget::getMaxLvl(void) {
    return spinMaxLvl->value();
}

void MainWidget::keyReleaseEvent(QKeyEvent *ev) {
    switch(ev->key()) {
    case Qt::Key_A:
        keyPressFlag[0] = false;
        break;
    case Qt::Key_S:
        keyPressFlag[1] = false;
        break;
    case Qt::Key_D:
        keyPressFlag[2] = false;
        break;
    case Qt::Key_F:
        keyPressFlag[3] = false;
        break;
    case Qt::Key_J:
        keyPressFlag[0 + 4] = false;
        break;
    case Qt::Key_K:
        keyPressFlag[1 + 4] = false;
        break;
    case Qt::Key_L:
        keyPressFlag[2 + 4] = false;
        break;
    case Qt::Key_Semicolon:
        keyPressFlag[3 + 4] = false;
        break;
    default:
        //qDebug() << ev->key();
        break;
    }
}

void MainWidget::keyPressEvent(QKeyEvent *ev) {
    if(lblGameWindow->isRunning() == false) {
        switch(ev->key()) {
        case Qt::Key_A:
            keyPressFlag[0] = true;
            break;
        case Qt::Key_S:
            keyPressFlag[1] = true;
            break;
        case Qt::Key_D:
            keyPressFlag[2] = true;
            break;
        case Qt::Key_F:
            keyPressFlag[3] = true;
            break;
        case Qt::Key_J:
            keyPressFlag[0 + 4] = true;
            break;
        case Qt::Key_K:
            keyPressFlag[1 + 4] = true;
            break;
        case Qt::Key_L:
            keyPressFlag[2 + 4] = true;
            break;
        case Qt::Key_Semicolon:
            keyPressFlag[3 + 4] = true;
            break;
        default:
            //qDebug() << ev->key();
            break;
        }

        if((keyPressFlag[0] && keyPressFlag[1] && keyPressFlag[2] && keyPressFlag[3]) ||
                (keyPressFlag[0+4] && keyPressFlag[1+4] && keyPressFlag[2+4] && keyPressFlag[3+4])) {
            lblGameWindow->setRunning(true);
        }
    } else {
        if(ev->isAutoRepeat())
            return;
        lblGameWindow->getKey(ev->text());
    }
}

void MainWidget::updateStatistic(int rightCnt, int wrongCnt) {
    qDebug() << rightCnt << wrongCnt;
    if(rightCnt == 0 && wrongCnt == 0)
    {
        lblStatistics->setText("正确率: 0% (0 / 0)");
    }
    else
    {
        lblStatistics->setText(QString().sprintf("正确率: %.1f%% ( %d / %d )", 100.0 * rightCnt / (rightCnt + wrongCnt), rightCnt, rightCnt + wrongCnt));
    }
}

MainWidget::~MainWidget()
{
}

